package com.yc.atm;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zwei
 * @create 2024-07-19 10:00
 */
public class Bank {
    private List<BankAccount>accounts=new ArrayList<>();
    public Bank(){
        //初始化一些账号
        for (int i = 1; i <=10 ; i++) {
            accounts.add(new BankAccount(i,10));
        }
    }
    //查询：无需加锁
    public BankAccount search(int id) throws Exception{
        for (BankAccount ba:accounts){
            if (ba.getId()==id){
                return ba;
            }
        }
        throw new Exception("查无此账号："+id);
    }
    //存款：加锁
    public BankAccount deposite(int id,double money) throws Exception {
        BankAccount ba=search(id);
        synchronized (ba){
            ba.setBalance(ba.getBalance()+money);
        }
        return ba;
    }
    //取款
    public BankAccount withdraw(int id,double money) throws Exception {
        BankAccount ba=search(id);
        synchronized (ba){
            if (money>ba.getBalance()){
                throw new Exception("余额不足");
            }
            ba.setBalance(ba.getBalance()-money);
        }
        return ba;
    }
}
